<?php
require('../include/init.inc.php');
$method = "";
extract($_REQUEST, EXTR_IF_EXISTS);

if ($method == 'upload') {
    $file = $_FILES["productupfile"];

    $max_size = "2000000"; //最大文件限制（单位：byte）
    $fname = $file['name'];

    $ftype = strtolower(substr(strrchr($fname, '.'), 1));
//文件格式
    $uploadfile = $file['tmp_name'];
    if (Common::isPost()) {
        if (is_uploaded_file($uploadfile)) {
            if ($file['size'] > $max_size) {
                OSAdmin::alert("error", "导入文件太大");
            }
            if ($ftype != 'xls' || $ftype != 'csv') {
                OSAdmin::alert("error", "文件类型错误");
            }
        } else {
            OSAdmin::alert("error", "文件为空");
        }
    }


    $file_arr = ExcelReader::readXLS($uploadfile);
    unset($file_arr[0]);


    //已有的产品就直接加数量，没有的产品就添加一条产品信息
    foreach ($file_arr as $data) {
        $product = AdminProducts::getProductById($data[0]);
        if ($product) {
            $rk_num = $product['products_sale_num'] + $data[5];
            $ck_num = $product['products_sale_other'] + $data[7];
            if ($data[11] == 1) {//入库
                $update_data = array("products_name" => $data[1], "products_price" => $data[2], "products_currency" => $data[3], "products_description" => $data[4], "products_sale_num" => $rk_num, "products_add_date" => date('Y-m-d H:i:s'), "products_add_status" => $data[6], "products_evaluate" => $data[9]);
            } else {//出库
                $update_data = array("products_name" => $data[1], "products_price" => $data[2], "products_currency" => $data[3], "products_description" => $data[4], "products_sale_other" => $ck_num, "products_due_date" => date('Y-m-d H:i:s'), "products_due_status" => $data[8], "products_evaluate" => $data[9]);
            }
            AdminProducts::update($data[0], $update_data);
        } else {
            $lastedInsertId = AdminId::lastedInsertId(array("createtime" => time()));
            $model_name = $data[10] . "_" . $lastedInsertId . "_" . Common::getRandChar(4);
            if ($data[11] == 1) {//入库
                $add_data = array("products_id" => $data[0], "products_name" => $data[1], "products_price" => $data[2], "products_currency" => $data[3], "products_description" => $data[4], "products_sale_num" => $data[5], "products_add_date" => date('Y-m-d H:i:s'), "products_add_status" => $data[6], "products_evaluate" => $data[9], "products_model" => $model_name);
            } else {
                $add_data = array("products_id" => $data[0], "products_name" => $data[1], "products_price" => $data[2], "products_currency" => $data[3], "products_description" => $data[4], "products_sale_other" => $data[7], "products_due_date" => date('Y-m-d H:i:s'), "products_due_status" => $data[8], "products_evaluate" => $data[9], "products_model" => $model_name);
            }
            AdminProducts::add($add_data);
        }
    }


    SysLog::addLog(UserSession::getUserName(), 'AddOrUpdate', 'ProductsBatchAdd', $method, json_encode($file));
    OSAdmin::alert("success", "导入成功");

}

Template::display('entpot/products_batch_add.tpl');